//
//  BaseData.m
//  OST
//
//  Created by mac on 13-7-11.
//  Copyright (c) 2013年 mac. All rights reserved.
//

#import "BaseData.h"

@implementation BaseData

@synthesize httpRequest;
@synthesize dataRecivedBlock;
@synthesize reciveData;
- (void)dealloc {
    
    [httpRequest clearDelegatesAndCancel];
}

+(BaseData *) sharedInstance
{
    return nil;
}

-(void)reLoadData
{

}

-(id)getResultData
{
    return nil;
}

-(void)doneLoadData:(id)data
{
    if(dataRecivedBlock){
		dataRecivedBlock(data);
    }
}

-(void)cleanData
{
    
}

-(void)requestStarted:(ASIHTTPRequest *)request
{
    self.reciveData = [NSMutableString string];
}

- (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data
{
    NSLog(@"didReceiveData");
    
    NSString *dataString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    dataString = [dataString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    [reciveData appendString:dataString];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
#warning 假冒数据
    self.reciveData = [NSMutableString string];
    [reciveData appendString:@"Default Layer/./0/.//./0/./"];
    [self requestFinished:request];
    
#warning 原来的
    //[self doneLoadData:nil];
}

-(void)reLoadDataWithDataRecivedBlock:(DataRecivedBlock)bolck
{
    self.dataRecivedBlock = bolck;
    
    [httpRequest clearDelegatesAndCancel];

    [self reLoadData];
}

-(void)getDataWithDataRecivedBlock:(DataRecivedBlock)bolck
{
    self.dataRecivedBlock = bolck;
    if ([self getResultData]) {
        dataRecivedBlock([self getResultData]);
    }else{
        [httpRequest clearDelegatesAndCancel];

        [self reLoadData];
    }
}


@end
